package com.work.game.ad;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.widget.Toast;

import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdConstant;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTAdSdk;
import com.bytedance.sdk.openadsdk.TTRewardVideoAd;
import com.bytedance.sdk.openadsdk.mediation.ad.MediationAdSlot;
import com.bytedance.sdk.openadsdk.mediation.manager.MediationAdEcpmInfo;
import com.bytedance.sdk.openadsdk.mediation.manager.MediationBaseManager;
import com.work.game.ad.inter.RewardAdInter;

import cn.hutool.core.util.ObjectUtil;

/**
 * 激励视频无回调
 */
public class MediationRewardAd {
    private Activity activity;
    private String codeId;
    private String userId;
    private RewardAdInter rewardAdInter;
    private String toastContent;
    private String ecpm;

    private TTRewardVideoAd ttRewardVideoAd;
    private boolean isRewardValid = false;

    private MediationRewardAd() {
    }

    public static volatile MediationRewardAd instance;

    public static synchronized MediationRewardAd getInstance() {
        if (ObjectUtil.isNull(instance)) {
            synchronized (MediationRewardAd.class) {
                if (ObjectUtil.isNull(instance)) {
                    instance = new MediationRewardAd();
                }
            }
        }
        return instance;
    }

    public void init(String codeId, String userId) {
        this.codeId = codeId;
        this.userId = userId;
    }

    public void setActivity(Activity activity) {
        this.activity = activity;
    }

    public void setRewardAdInter(RewardAdInter rewardAdInter) {
        this.rewardAdInter = rewardAdInter;
    }

    public void setToastContent(String toastContent) {
        this.toastContent = toastContent;
    }

    //激励视频 start
    private AdSlot buildRewardAdlost() {
        return new AdSlot.Builder().setCodeId(codeId).setAdCount(3)//广告位ID
                .setUserID(userId).setOrientation(TTAdConstant.VERTICAL)  //激励视频方向
                .setMediationAdSlot(new MediationAdSlot.Builder().setMuted(false).build()).build();
    }

    //加载激励视频
    public void loadCacheRewardAd() {
        TTAdNative adNativeLoader = TTAdSdk.getAdManager().createAdNative(activity);
        /** 这里为激励视频的简单功能，如需使用复杂功能，如gromore的服务端奖励验证，请参考demo中的AdUtils.kt类中激励部分 */
        adNativeLoader.loadRewardVideoAd(buildRewardAdlost(), new TTAdNative.RewardVideoAdListener() {
            @Override
            public void onError(int errorCode, String errorMsg) {
                //广告加载失败
                //Toast.makeText(activity, errorMsg, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onRewardVideoAdLoad(TTRewardVideoAd ttRewardVideoAd) {
                //广告加载失败
            }

            @Override
            public void onRewardVideoCached() {
                //广告缓存成功 此api已经废弃，请使用onRewardVideoCached(TTRewardVideoAd ttRewardVideoAd)
            }

            @Override
            public void onRewardVideoCached(TTRewardVideoAd ttRewardVideoAd) {
                //广告缓存成功 在此回调中进行广告展示
                getInstance().ttRewardVideoAd = ttRewardVideoAd;
            }
        });
    }

    //展示激励视频
    public void showCacheRewardAd(AlertDialog loadingDailog) {
        if (ttRewardVideoAd == null) {
            loadRewardAd(loadingDailog);
            return;
        }
        ttRewardVideoAd.setRewardAdInteractionListener(new TTRewardVideoAd.RewardAdInteractionListener() {
            @Override
            public void onAdShow() {
                //广告展示
                //获取展示广告相关信息，需要再show回调之后进行获取
                MediationBaseManager manager = ttRewardVideoAd.getMediationManager();
                if (manager != null && manager.getShowEcpm() != null) {
                    MediationAdEcpmInfo showEcpm = manager.getShowEcpm();
                    ecpm = showEcpm.getEcpm(); //展示广告的价格
                }
            }

            @Override
            public void onAdVideoBarClick() {
                //广告点击
            }

            @Override
            public void onAdClose() {
                //广告关闭
                if (getInstance().isRewardValid) {
                    rewardAdInter.rewardAdSign(ecpm);
                    getInstance().isRewardValid = false;
                } else {
                    Toast.makeText(activity, toastContent, Toast.LENGTH_LONG).show();
                }
                onDestroy();
                loadCacheRewardAd();
            }

            @Override
            public void onVideoComplete() {
                //广告视频播放完成

            }

            @Override
            public void onVideoError() {
                //广告视频错误
            }

            @Override
            public void onRewardVerify(boolean rewardVerify, int rewardAmount, String rewardName, int errorCode, String errorMsg) {
                //奖励发放 已废弃 请使用 onRewardArrived 替代
            }

            @Override
            public void onRewardArrived(boolean isRewardValid, int rewardType, Bundle extraInfo) {
                getInstance().isRewardValid = isRewardValid;
            }

            @Override
            public void onSkippedVideo() {
            }
        });
        if (ttRewardVideoAd.getMediationManager().isReady()) {
            if (ObjectUtil.isNotNull(loadingDailog)) {
                loadingDailog.dismiss();
            }
            ttRewardVideoAd.showRewardVideoAd(activity); //展示激励视频
        }else{
            loadRewardAd(loadingDailog);
        }
    }
    //激励视频 end

    //加载激励视频
    private void loadRewardAd(AlertDialog loadingDailog) {
        TTAdNative adNativeLoader = TTAdSdk.getAdManager().createAdNative(activity);
        /** 这里为激励视频的简单功能，如需使用复杂功能，如gromore的服务端奖励验证，请参考demo中的AdUtils.kt类中激励部分 */
        adNativeLoader.loadRewardVideoAd(buildRewardAdlost(), new TTAdNative.RewardVideoAdListener() {
            @Override
            public void onError(int errorCode, String errorMsg) {
                //广告加载失败
                Toast.makeText(activity, "视频加载失败", Toast.LENGTH_SHORT).show();
                loadingDailog.dismiss();

            }

            @Override
            public void onRewardVideoAdLoad(TTRewardVideoAd ttRewardVideoAd) {
                //广告加载失败
            }

            @Override
            public void onRewardVideoCached() {
                //广告缓存成功 此api已经废弃，请使用onRewardVideoCached(TTRewardVideoAd ttRewardVideoAd)
            }

            @Override
            public void onRewardVideoCached(TTRewardVideoAd ttRewardVideoAd) {
                //广告缓存成功 在此回调中进行广告展示
                showRewardAd(loadingDailog, ttRewardVideoAd);
            }
        });
    }

    //展示激励视频
    private void showRewardAd(AlertDialog loadingDailog, TTRewardVideoAd ttRewardVideoAd) {
        if (ttRewardVideoAd == null) {
            return;
        }
        ttRewardVideoAd.setRewardAdInteractionListener(new TTRewardVideoAd.RewardAdInteractionListener() {
            @Override
            public void onAdShow() {
                //广告展示
                //获取展示广告相关信息，需要再show回调之后进行获取
                MediationBaseManager manager = ttRewardVideoAd.getMediationManager();
                if (manager != null && manager.getShowEcpm() != null) {
                    MediationAdEcpmInfo showEcpm = manager.getShowEcpm();
                    ecpm = showEcpm.getEcpm(); //展示广告的价格
                }
            }

            @Override
            public void onAdVideoBarClick() {
                //广告点击
            }

            @Override
            public void onAdClose() {
                //广告关闭
                if (getInstance().isRewardValid) {
                    rewardAdInter.rewardAdSign(ecpm);
                } else {
                    Toast.makeText(activity, toastContent, Toast.LENGTH_SHORT).show();
                }
                onDestroy();
                loadCacheRewardAd();
            }

            @Override
            public void onVideoComplete() {
                //广告视频播放完成

            }

            @Override
            public void onVideoError() {
                //广告视频错误
            }

            @Override
            public void onRewardVerify(boolean rewardVerify, int rewardAmount, String rewardName, int errorCode, String errorMsg) {
                //奖励发放 已废弃 请使用 onRewardArrived 替代
            }

            @Override
            public void onRewardArrived(boolean isRewardValid, int rewardType, Bundle extraInfo) {
                getInstance().isRewardValid = isRewardValid;
            }

            @Override
            public void onSkippedVideo() {
            }
        });
        if (ObjectUtil.isNotNull(loadingDailog)) {
            loadingDailog.dismiss();
        }
        ttRewardVideoAd.showRewardVideoAd(activity); //展示激励视频
    }

    private void onDestroy() {
        if (ttRewardVideoAd != null && ttRewardVideoAd.getMediationManager() != null) {
            ttRewardVideoAd.getMediationManager().destroy();
        }
        ttRewardVideoAd = null;
    }
}
